/*************************************************************
 * File: Validator.java
 * Copyright (c) 2012
 * Author: Huang Shaobin
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Feb 20, 2013
 * Description: 
 ************************************************************/

package snow.core.toolkit;

import java.util.Collection;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import snow.core.exceptions.BusinessException;

/**
 * 校验工具
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
public class Validator {

	public static void isAssignable(Class<?> superType, Class<?> subType, String code, String message) throws BusinessException {
		notNull(superType, code, message);
		if (subType == null || !superType.isAssignableFrom(subType)) {
			throw new BusinessException(code, message);
		}
	}

	public static void isEmpty(Collection<?> collection, String code, String message) throws BusinessException {
		if (CollectionUtils.isNotEmpty(collection)) {
			throw new BusinessException(code, message);
		}
	}

	public static void isEmpty(Map<?, ?> map, String code, String message) throws BusinessException {
		if (MapUtils.isNotEmpty(map)) {
			throw new BusinessException(code, message);
		}
	}

	public static void isInstanceOf(Class<?> type, Object obj, String code, String message) throws BusinessException {
		notNull(type, code, message);
		if (!type.isInstance(obj)) {
			throw new BusinessException(code, message);
		}
	}

	public static void isNull(Object object, String code, String message) throws BusinessException {
		if (object != null) {
			throw new BusinessException(code, message);
		}
	}

	public static void isTrue(boolean expression, String code, String message) throws BusinessException {
		if (!expression) {
			throw new BusinessException(code, message);
		}
	}

	public static void noNullElements(Object[] array, String code, String message) throws BusinessException {
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				if (array[i] == null) {
					throw new BusinessException(code, message);
				}
			}
		}
	}

	public static void notBlank(String text, String code, String message) throws BusinessException {
		if (StringUtils.isBlank(text)) {
			throw new BusinessException(code, message);
		}
	}

	public static void notEmpty(Collection<?> collection, String code, String message) throws BusinessException {
		if (CollectionUtils.isEmpty(collection)) {
			throw new BusinessException(code, message);
		}
	}

	public static void notEmpty(Map<?, ?> map, String code, String message) throws BusinessException {
		if (MapUtils.isEmpty(map)) {
			throw new BusinessException(code, message);
		}
	}

	public static void notEmpty(Object[] array, String code, String message) throws BusinessException {
		if (ArrayUtils.isEmpty(array)) {
			throw new BusinessException(code, message);
		}
	}

	public static void notEmpty(String text, String code, String message) throws BusinessException {
		if (StringUtils.isEmpty(text)) {
			throw new BusinessException(code, message);
		}
	}

	public static void notNull(Object object, String code, String message) throws BusinessException {
		if (object == null) {
			throw new BusinessException(code, message);
		}
	}

	public static void state(boolean expression, String code, String message) throws BusinessException {
		if (!expression) {
			throw new BusinessException(code, message);
		}
	}

}
